<template>
  <div>
    <el-row :gutter="4" style="height: 100%">
      <el-col :span="5" style="height: 100%">
        <div style="height: 100%; overflow: auto">
          <el-tree
            class="tree"
            :data="flowTree"
            :props="{ children: 'sons', label: 'label' }"
            :default-expand-all="true"
            :expand-on-click-node="false"
            @node-click="treeClick"
            highlight-current
          ></el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div>
          <div class="header">
            <span style="margin-left: 20px">表单搜索</span>
            <el-input
              placeholder="请输入关键字搜索表单"
              v-model="req.keyword"
              style="width: 200px; margin-left: 10px"
            ></el-input>
            <el-button
              type="primary"
              style="margin-left: 34px"
              size="small"
              @click="getMyHandledForms()"
            >
              搜索
            </el-button>
          </div>
          <div class="main">
            <el-table
              style="overflow: auto"
              height="100%"
              :data="res.Items"
              stripe
              border
              highlight-current-row
            >
              <el-table-column label="流程" width="180px">
                <template slot-scope="scope">
                  <div style="margin-bottom: 5px">
                    <el-tag type="primary" v-if="scope.row.WFM_Status == 1">
                      {{ scope.row.WFM_StatusDesc }}[{{ scope.row.FRS_Name }}]
                    </el-tag>
                    <el-tag
                      type="success"
                      v-else-if="scope.row.WFM_Status == 2"
                    >
                      {{ scope.row.WFM_StatusDesc }}
                    </el-tag>
                    <el-tag type="danger" v-else>
                      {{ scope.row.WFM_StatusDesc }}
                    </el-tag>
                  </div>
                  <span>{{ scope.row.FLOW_Name }}</span>
                  <!-- <el-link type="primary">状态：{{ scope.row.FRS_Name }}</el-link> -->
                </template>
              </el-table-column>
              <el-table-column label="发起人及日期" width="140px">
                <template slot-scope="scope">
                  <div>{{ scope.row.USR_Name }}</div>
                  {{ scope.row.WFM_CreateTime | formatDate }}
                </template>
              </el-table-column>
              <el-table-column label="表单名" width="300px">
                <template slot-scope="scope">
                  <span>{{ scope.row.WFM_FormDataName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="表单信息">
                <template slot-scope="scope">
                  <div v-if="scope.row.WFM_FormDataCode">
                    搜寻码：{{ scope.row.WFM_FormDataCode }}
                  </div>
                  <div v-for="item in scope.row.FieldValues" :key="item.FFV_ID">
                    {{ item.FF_DisplayName }}: {{ item.FFV_DisplayValue }}
                  </div>
                </template>
              </el-table-column>
              <!--<el-table-column label="属性1" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop1Name }}: {{ scope.row.WFM_Prop1 }}
              </template>
            </el-table-column>
            <el-table-column label="属性2" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop2Name }}: {{ scope.row.WFM_Prop2 }}
              </template>
            </el-table-column>
            <el-table-column label="属性3" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop3Name }}: {{ scope.row.WFM_Prop3 }}
              </template>
            </el-table-column>
            <el-table-column label="属性4" width="250px">
              <template slot-scope="scope">
                {{ scope.row.WFM_Prop4Name }}: {{ scope.row.WFM_Prop4 }}
              </template>
            </el-table-column> -->
              <el-table-column fixed="right" label="操作" width="100px">
                <template slot-scope="scope">
                  <div>
                    <el-button
                      type="text"
                      icon="el-icon-search"
                      @click="toFormPage(scope.row.WFM_ID)"
                    >
                      查看
                    </el-button>
                  </div>
                  <div>
                    <el-button
                      type="text"
                      icon="el-icon-position"
                      @click="
                        curForm = scope.row
                        sharedEmpVis = true
                      "
                    >
                      分享
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div style="width: 100%; height: 40px">
            <el-pagination
              layout="total,prev,pager,next"
              style="float: right"
              background
              small
              :current-page.sync="req.pageIndex"
              :total="res.Total"
              :page-size="req.pageSize"
              :page-sizes="[10, 15, 20]"
              @size-change="
                (val) => {
                  req.pageSize = val
                  getMyHandledForms()
                }
              "
              @current-change="getMyHandledForms()"
            ></el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-dialog
      :visible.sync="sharedEmpVis"
      title="选择成员"
      width="60vw"
      :modal-append-to-body="false"
      top="70px"
    >
      <companyEmployeesChoose @sure="shareForm" />
    </el-dialog>
  </div>
</template>
<script>
import {
  getCompanyFlowTree,
  getMyHandledForms,
  shareForm,
} from '@/api/flowCenter/form/formManage'
import companyEmployeesChoose from '@/views/web/orgnization/components/companyEmployeesChoose.vue'
export default {
  components: { companyEmployeesChoose },
  data: function () {
    return {
      curNode: null,
      flowTree: [],
      req: {
        keyword: null,
        bsnId: null,
        flowId: null,
        pageIndex: 1,
        pageSize: 10,
      },
      res: {
        Items: [],
        Total: 0,
      },
      sharedEmpVis: false,
    }
  },
  methods: {
    treeClick(item) {
      this.curNode = item
      this.getMyHandledForms()
    },
    getMyHandledForms() {
      if (this.curNode) {
        switch (this.curNode.level) {
          case 0: {
            this.req.bsnId = null
            this.req.flowId = null
            break
          }
          case 1: {
            this.req.bsnId = this.curNode.id
            this.req.flowId = null
            break
          }
          case 2: {
            this.req.bsnId = null
            this.req.flowId = this.curNode.id
            break
          }
        }
      }
      getMyHandledForms(this.req).then(({ data }) => {
        this.res = data
      })
    },
    getCompanyFlowTree() {
      getCompanyFlowTree().then(({ data }) => {
        this.flowTree = data
        this.getMyHandledForms()
      })
    },
    shareForm(val) {
      if (this.curForm) {
        this.$prompt('请输入留言', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        })
          .then(({ value }) => {
            shareForm({
              formId: this.curForm.WFM_ID,
              emps: val,
              comment: value,
            }).then(() => {
              this.$message('分享成功')
              this.sharedEmpVis = false
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '取消分享',
            })
          })
      }
    },
    toFormPage(formId) {
      this.$router.push({
        name: 'formInfoPage',
        query: { formId },
      })
    },
  },
  mounted() {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
    this.getCompanyFlowTree()
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>

<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
.main {
  height: calc(75vh - 60px);
  overflow: auto;
}
</style>
